package com.wangl.generator.wlmybatisgenerator.persistent;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.wangl.generator.wlmybatisgenerator.data.ConnectionBean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName ConnectionPersistent
 * @Description 连接持久化
 * @Author wangl
 * @Date 2021/12/3 10:50
 */
@State(name = "ConnectionPersistent", storages = {@Storage("wlMybatisGenerator/connections.xml")})
public class ConnectionPersistent implements PersistentStateComponent<ConnectionPersistent> {

    private List<ConnectionBean> connections;

    @NotNull
    public static ConnectionPersistent getInstance(Project project) {
        ConnectionPersistent service = project.getService(ConnectionPersistent.class);
        // 配置的默认值
        if (service.connections == null) {
            // 默认配置
            service.connections = new LinkedList<>();
        }
        return service;
    }

    @Override
    public @Nullable ConnectionPersistent getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull ConnectionPersistent state) {
        XmlSerializerUtil.copyBean(state, this);
    }

    public List<ConnectionBean> getConnections() {
        return connections;
    }

    public void setConnections(List<ConnectionBean> connections) {
        this.connections = connections;
    }
}
